package com.meet.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.meet.app.R;
import com.meet.app.model.AddFriendModel;
import com.meet.app.ui.AddFriendActivity;
import com.meet.framework.helper.GlideHelper;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class AddFriendAdpater extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    //标题
    public  static final int TYPE_TITLE=0;
    //内容
    public  static final int TYPE_CONTENT=1;

    private Context mContext;

    private List<AddFriendModel> mList;

    private LayoutInflater inflater;

    private OnclickListener onClickListener;

    public void setOnClickListener(OnclickListener onClickListener){
        this.onClickListener=onClickListener;
    }

    public AddFriendAdpater(Context mContext,List<AddFriendModel> mList){
        this.mContext=mContext;
        this.mList=mList;
        inflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==TYPE_TITLE){
            return new TitleViewHolder(inflater.inflate(R.layout.layout_search_title_item,null));
        }
        if(viewType==TYPE_CONTENT){
            return new ContentViewHolder(inflater.inflate(R.layout.layout_search_user_item,null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        AddFriendModel model=mList.get(position);
        if(model.getType()==TYPE_TITLE){
            ((TitleViewHolder)holder).tvTitle.setText(model.getTitle());
        }else if(model.getType()==TYPE_CONTENT){
            //设置头像
            ContentViewHolder viewHolder=((ContentViewHolder)holder);
            GlideHelper.loadUrl(mContext,model.getPhoto(), ((ContentViewHolder)holder).tvPhoto);
            //设置性别
            viewHolder.ivSex.setImageResource(model.isSex() ? R.drawable.img_boy_icon : R.drawable.img_girl_icon);
            //设置昵称
            viewHolder.nickname.setText(model.getNickName());
//            //年龄
            viewHolder.age.setText(model.getAge() + this.mContext.getString( R.string.text_search_age));
            //设置描述
            viewHolder.tvDesc.setText(model.getDesc());

            //通讯录
            if(model.isContact()){
                viewHolder.ll_contact_info.setVisibility(View.VISIBLE);
                viewHolder.tv_contact_name.setText(model.getContactName());
                viewHolder.tv_contact_phone.setText(model.getContactPhone());
            }

            //点击事件
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    UserInfoActivity.startActivity(AddFriendActivity.this,
//                            model.getUserId());
                    if(onClickListener!=null){
                        onClickListener.onClick(position);
                    }
                }
            });
        }
    }

    public  interface OnclickListener{
        void onClick(int position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getType();
    }

    class TitleViewHolder extends  RecyclerView.ViewHolder{
        private TextView tvTitle;

        public TitleViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle=itemView.findViewById(R.id.tv_title);
        }
    }

    class ContentViewHolder extends  RecyclerView.ViewHolder{
        private CircleImageView tvPhoto;
        private ImageView ivSex;
        private TextView nickname;
        private TextView age;
        private TextView tvDesc;

        private LinearLayout ll_contact_info;
        private TextView tv_contact_name;
        private TextView tv_contact_phone;

        public ContentViewHolder(@NonNull View itemView) {
            super(itemView);
            tvPhoto=itemView.findViewById(R.id.iv_photo);
            ivSex=itemView.findViewById(R.id.iv_sex);
            nickname=itemView.findViewById(R.id.tv_nickname);
            age=itemView.findViewById(R.id.tv_age);
            tvDesc=itemView.findViewById(R.id.tv_desc);

            ll_contact_info=itemView.findViewById(R.id.ll_contact_info);
            tv_contact_name=itemView.findViewById(R.id.tv_contact_name);
            tv_contact_phone=itemView.findViewById(R.id.tv_contact_phone);
        }
    }
}
